package com.gzk.oeno.ihm.vin.form;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.gzk.oeno.business.cepage.Cepage;
import com.gzk.oeno.business.cepage.CepageManager;
import com.gzk.oeno.ihm.common.HibernateEditForm;
import com.gzk.oeno.tech.ui.Formator;
import com.gzk.utils.CustomException;
import com.gzk.utils.ihm.UIUtils;
import com.gzk.utils.misc.StringUtils;

public class CepageForm extends HibernateEditForm<Integer, Cepage>{

	private JTextField nameField;
	private ButtonGroup colorGroup;
	private JRadioButton blancButton;
	private JRadioButton rougeButton;
	private JTextArea autresNomsArea;
	private JTextArea descArea;
	
	
	
	public CepageForm() {
		super(CepageManager.getInstance());
	}

	@Override
	protected JPanel buildForm() {
		JPanel pane = new JPanel(new GridBagLayout());
		
		JPanel typePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		typePanel.add(getBlancButton());
		typePanel.add(getRougeButton());
		
		GridBagConstraints gc = getNewConstraint();
		addLabelAndField(pane, Formator.boldLabel("Nom* :"), getNameField(), gc);
		addLabelAndField(pane, new JLabel("Type :"), typePanel, gc);
		addLabelAndField(pane, new JLabel("Autres noms :"), getAutresNomsArea(), gc);
		addLabelAndField(pane, new JLabel("Description :"), getDescArea(), gc);

		return pane;
	}

	@Override
	protected Cepage fromForm() throws CustomException {
		Cepage cepage = original != null ? original : new Cepage();
		
		Boolean isRouge = null;
		if (getBlancButton().isSelected()){
			isRouge = Boolean.FALSE;
		} else if (getRougeButton().isSelected()){
			isRouge = Boolean.TRUE;
		}
		
		cepage.setName(getNameField().getText());
		cepage.setIsRouge(isRouge);
		cepage.setAutresNoms(getAutresNomsArea().getText());
		cepage.setDescription(getDescArea().getText());
		
		return cepage;
	}

	@Override
	protected void toForm(Cepage object) {
		getNameField().setText(object.getName());
		getBlancButton().setSelected(object.getIsRouge() != null && !object.getIsRouge());
		getRougeButton().setSelected(object.getIsRouge() != null && object.getIsRouge());
		getAutresNomsArea().setText(object.getAutresNoms());
		getDescArea().setText(object.getDescription());
	}

	@Override
	protected void cleanForm() {
		getNameField().setText(null);
		getBlancButton().setSelected(false);
		getRougeButton().setSelected(false);
		getAutresNomsArea().setText(null);
		getDescArea().setText(null);
	}

	@Override
	protected boolean isEditable() {
		return StringUtils.isNotEmpty(getNameField().getText());
	}

	protected JTextField getNameField() {
		if (nameField == null){
			nameField = new JTextField(25);
			nameField.addKeyListener(autoRefreshKListener);
		}
		return nameField;
	}

	protected ButtonGroup getColorGroup(){
		if (colorGroup == null){
			colorGroup = new ButtonGroup();
		}
		return colorGroup;
	}
	
	protected JRadioButton getBlancButton() {
		if (blancButton == null){
			blancButton = new JRadioButton("Raisin blanc", UIUtils.loadSilentIcon("icons/white_grapes_16.png"));
			getColorGroup().add(blancButton);
		}
		return blancButton;
	}

	protected JRadioButton getRougeButton() {
		if (rougeButton == null){
			rougeButton = new JRadioButton("Raisin rouge", UIUtils.loadSilentIcon("icons/grapes_16.png"));
			getColorGroup().add(rougeButton);
		}
		return rougeButton;
	}

	protected JTextArea getAutresNomsArea() {
		if (autresNomsArea == null){
			autresNomsArea = new JTextArea(2, 25);
		}
		return autresNomsArea;
	}

	protected JTextArea getDescArea() {
		if (descArea == null){
			descArea = new JTextArea(5, 25);
		}
		return descArea;
	}

}
